package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjFaultSolution;
import com.zb.mapper.ZjFaultSolutionMapper;
import com.zb.service.ZjFaultSolutionService;
import com.zb.vo.FaultAnnexVo;
import nonapi.io.github.classgraph.json.Id;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 故障解决方案表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-31
 */
@Service
public class ZjFaultSolutionServiceImpl extends ServiceImpl<ZjFaultSolutionMapper, ZjFaultSolution> implements ZjFaultSolutionService {
    @Resource
    private ZjFaultSolutionMapper faultSolutionMapper;

    // 保存解决方案
    @Override
    public void save(Long custAccId, Long faultId, List<FaultAnnexVo> solutions, List<Long> delIds) {
        if (null != delIds && !delIds.isEmpty()) {
            this.removeByIds(delIds);
        }
        if (null == solutions || solutions.isEmpty()) {
            return;
        }
        List<ZjFaultSolution> adds = new ArrayList<>();
        List<ZjFaultSolution> updates = new ArrayList<>();
        ZjFaultSolution solution = null;
        for (FaultAnnexVo s : solutions) {
            solution = new ZjFaultSolution();
            BeanUtils.copyProperties(s, solution);
            if (null != solution.getId()) {
                updates.add(solution);
                continue;
            }
            solution.setId(IdWorker.getId());
            solution.setCustAccId(custAccId);
            solution.setFaultId(faultId);
            adds.add(solution);
        }
        if (!adds.isEmpty()) {
            this.saveBatch(adds);
        }
        // 先预留，暂时没有修改的
//        if (!updates.isEmpty()) {
//            this.updateBatchById(updates);
//        }
    }

    // 根据故障ID查询
    @Override
    public List<ZjFaultSolution> getByFaultId(Long faultId) {
        QueryWrapper<ZjFaultSolution> wrapper = new QueryWrapper<>();
        wrapper.select("id", "path");
        wrapper.eq("fault_id", faultId);
        return this.list(wrapper);
    }

    // 根据故障ID删除
    @Override
    public void deleteBatchByFaultId(List<Long> faultIds) {
        if (CollectionUtil.isEmpty(faultIds)) {
            return;
        }
        QueryWrapper<ZjFaultSolution> wrapper = new QueryWrapper<>();
        wrapper.in("fault_id", faultIds);
        this.remove(wrapper);
    }
}
